package com.sunnada.nginx.controller;

import com.alibaba.fastjson.JSON;
import com.sunnada.nginx.bean.nginx.http.Location;
import com.sunnada.nginx.exception.CustonException;
import com.sunnada.nginx.service.RuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@Api(value = "转发规则", description = "转发规则")
@RestController
@RequestMapping("rules")
public class RuleController {

    @Autowired
    RuleService ruleService;

    @ApiOperation(value = "获取转发规则列表", notes = "获取转发规则列表", httpMethod = "GET")
    @ApiImplicitParam(name = "nameAndPort", value = "服务器名称和端口", required = true, dataType = "String", paramType = "query", example = "localhost:80")
    @GetMapping
    @ResponseBody
    public Object list(String nameAndPort) throws CustonException {
        return ruleService.list(nameAndPort);
    }

    @ApiOperation(value = "获取转发规则", notes = "获取转发规则", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "监听名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "port", value = "监听接口", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "rule", value = "转发规则名称", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("{rule}")
    @ResponseBody
    public Object get(String name, String port, @PathVariable String rule) throws CustonException {
        if (StringUtils.isBlank(name) || StringUtils.isBlank(port) || StringUtils.isBlank(rule))
            throw new CustonException("参数不能为空");
        return ruleService.get(name, port, rule);
    }

    @ApiOperation(value = "新增转发规则", notes = "新增转发规则", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "监听名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "port", value = "监听接口", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping
    @ResponseBody
    public String post(String name, String port, Location location) throws CustonException {
        if (StringUtils.isBlank(name) || StringUtils.isBlank(port))
            throw new CustonException("参数不能为空");
        return ruleService.post(name, port, location) ? "success" : "fail";
    }


    @ApiOperation(value = "修改转发规则", notes = "修改转发规则", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "监听名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "port", value = "监听接口", required = true, dataType = "String", paramType = "query"),
    })
    @PutMapping
    @ResponseBody
    public String put(String name, String port, Location location) throws CustonException {
        if (StringUtils.isBlank(name) || StringUtils.isBlank(port))
            throw new CustonException("参数不能为空");
        return ruleService.put(name, port, location) ? "success" : "fail";
    }


    @ApiOperation(value = "删除转发规则", notes = "删除转发规则", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "监听名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "port", value = "监听接口", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "rule", value = "转发规则名称", required = true, dataType = "String", paramType = "path")
    })
    @DeleteMapping("{rule}")
    @ResponseBody
    public String delete(String name, String port, @PathVariable String rule) throws CustonException {
        if (StringUtils.isBlank(name) || StringUtils.isBlank(port) || StringUtils.isBlank(rule))
            throw new CustonException("参数不能为空");
        return ruleService.delete(name, port, rule) ? "success" : "fail";
    }
}
